/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author Nam
 */
@Entity
@NamedQueries({@NamedQuery(name = "OrderDetail.findByOrder", query = "SELECT i FROM OrderDetail i where i.pOrder=:pOrder")})
@IdClass(OrderDetailId.class)
public class OrderDetail implements ICommonEntity, Serializable {

    private static final long serialVersionUID = 1L;

    private int amount;

    @Id
    @ManyToOne
    private POrder pOrder;

    @Id
    @ManyToOne
    private Product product;

    public POrder getpOrder() {
        return pOrder;
    }

    public void setpOrder(POrder pOrder) {
        this.pOrder = pOrder;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "OrderDetail{" + "pOrder=" + pOrder + ", product=" + product + ", amount=" + amount + '}';
    }

    @Override
    public Object getId() {
        if (pOrder == null || product == null || pOrder.getId() == null || product.getId() == null) {
            return null;
        }
        return new OrderDetailId(pOrder.getId(), product.getId());
    }
}
